<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/2 0002
 * Time: 上午 11:11
 */

namespace DataAPI\Model;

use DataAPI\Extend\Xilian;
use DataAPI\Model\ErpApiModel;
use Think\Model;


class WestPayModel extends Model
{
    public $xilian;
    public $model;
    public $erpApiModel;
    private $appID = '10002';
    private $key    = 'Ukk4t40pR58ldy48vP694uOnBwsDotqJVP06V1dIma05xmj5ZT';
    private $organ = '0000';
    function __construct(ErpApiModel $erpApiModel)
    {
        set_time_limit(0);
        $this->xilian = Xilian::init();
        $this->model = M('supplier_products');
        $this->erpApiModel = $erpApiModel;
    }
    /**
     * 商品即时库存查询
     * @param type $code
     * @param type $organ
     */
    public function query_stock($code){
        return $this->xilian->query_stock($code,$this->organ);
    }
    /**
     * 商品账库存查询
     * @param type $page
     */
    public function query_account_stock(){
        return $this->xilian->query_account_stock();
    }
    /**
     * 获取品牌资料
     */
    public function getBrand(){
        return $this->xilian->get_brands();
    }
    /**
     * 获取商品类别资料
     */
    public function getCategory(){
        return $this->xilian->get_sort();
    }
    /**
     * @param $code
     * @return bool
     * 获取门店商品资料 并保存建议零售价
     */
    public function getShopGoodsInfo(){
        for ($i = 1;$i<=10000;$i++)
        {
            $shopGoods['data'] = $this->xilian->get_commdity_shop($i);
            var_dump($shopGoods);
            if(count($shopGoods['data'] > 0)) {
                foreach ($shopGoods['data'] as $k => $v) {
                 $this->model->where(['eran13' => $v['Code'],'is_sync'=>0])->save(['price'=>$v['Price']]);
                }
            }
        }
    }
    /*
     * 获取商品详细信息
     */
    public function getGoodsInfo($code){
        $info =  $this->xilian->get_commodity_info($code);
        return $info;
    }
    /**
     *获取门店信息
     */
    public function getShopInfo(){
        return $this->xilian->get_shop();
    }
    /**
     * @param $times
     * @return bool
     * 获取商品列表并插入本地tmp表
     */
    public function get_goods_list($start,$end){
        for ($i = $start; $i <= $end; $i++) {
            $result = $this->xilian->get_commodity($i);
            echo $i.'<br />';
            $arr = array(
                'msg' =>'获取商品信息成功',
                'data'=>array(
                    array(
                        'Code' => '4972422030537',
                        'Name' => '万特力护膝 M 黑色 自发热',
                        'Spec' => '件',
                        'Unit' => '件',
                        'Conversion' => '1',
                        'Jjfs' => '1',
                        'Sort' => '32080402',
                        'Mark' => '320001',
                        'Producer' => '日本',
                        'Keepday' => '1095'
                    ),
                    array(
                        'Code' => '4972422030539',
                        'Name' => '万特力护膝 M 黑色 自发热',
                        'Spec' => '件',
                        'Unit' => '件',
                        'Conversion' => '1',
                        'Jjfs' => '1',
                        'Sort' => '32080402',
                        'Mark' => '320001',
                        'Producer' => '日本',
                        'Keepday' => '1095'
                    ),
                    array(
                        'Code' => '4972422030538',
                        'Name' => '万特力护膝 M 黑色 自发热',
                        'Spec' => '件',
                        'Unit' => '件',
                        'Conversion' => '1',
                        'Jjfs' => '1',
                        'Sort' => '32080402',
                        'Mark' => '320001',
                        'Producer' => '日本',
                        'Keepday' => '1095'
                    ),
                    array(
                        'Code' => '4972422030534',
                        'Name' => '万特力护膝 M 黑色 自发热',
                        'Spec' => '件',
                        'Unit' => '件',
                        'Conversion' => '1',
                        'Jjfs' => '1',
                        'Sort' => '32080402',
                        'Mark' => '320001',
                        'Producer' => '日本',
                        'Keepday' => '1095'
                    )
                )
            );
            $res = $result['data'];
            var_dump($res);
            if(count($res)>0){
                foreach ($res as $k => $v){
                    if( !$this->model->where(array('earn13' => $v['Code']))->find() ) {
                       $info =  $this->model->add( array(
                            'app_id'=>$this->appID,
                            'earn13'        =>$v['Code'],
                            'name'          => $v['Name'],
                            'info_keywords' => substr($v['Name'],0,30),
                            'weight'        => $v['Spec'],
                            'pom'           => $v['Unit'],
                            'createdtime'   => time()
                        ) );
                        echo "添加商品<span>{$v['Code']}</span>成功<br />";
                    }
                    unset($res[$k]);
                }
                return $res;
            }
            else
            {
                $result['length']=0;
                var_dump($result);
               // return false;
            }
        }
    }

    /**
     * 判断店铺是否存在
     */
    public function is_shop_exits(){
        $shop_info = $this->xilian->get_shop();
        foreach( $shop_info['data'] as $m => $n ) {
            //判断店铺是否存在
            if( !M('suplier')->where(array('name' =>  $n['Name'], 'code' => $n['Organ']))->find() ) {
                //不存在 添加
                $insert_id = M('suplier')->add(
                    array(
                        'name' => $n['Name'],
                        'code' => $n['Organ'],
                        'user'  => '西联',
                        'tel' => $n['Organ']
                    )
                );
                $this->erpApiModel->supplier_add( $insert_id ); //erp添加供应商
            }
        }
    }

    /**
    * 获取并更新多个商品市场价 原价 和 库存
    */
    public function getPriceAndStock(){
        $codes =$this->model->where(['is_sync'=>0,'app_id'=>$this->appID])->getField('earn13',true);
        $codes = implode(',',$codes);
        echo $codes;
        $result = $this->xilian->query_stock_and_price($codes,$this->organ);
        var_dump($result);
        $res = $result['Data']['StockList'];
        foreach ($res as  $k => $v){
            $this->model->where(['app_id'=>$this->appID,'earn13'=>$v['Code']])->save([
                'market_price' => $v['OrigPrice'],
                'price' => $v['Price'],
                'stock_num' => $v['Stock']
            ]);
        }
    }
    /**
     * 获取商品价格 库存 并跟新市场价和建议零售价
     */
    public function getPrice(){
        $codes = $this->model->where(['is_sync'=>'0','app_id'=>$this->appID])->getField('earn13',true);
        foreach ($codes as $v)
        {
            //正常售价
           $result =  $this->xilian->query_price($v,$this->organ);
           print_r($result);
           echo '<hr / >';
            $this->model->where(['app_id'=>$this->appID,'earn13'=>$v])->save([
                'lock_price' =>$result['data']['OrigPrice'] * 3,
                'wholesale_price' =>$result['data']['OrigPrice'] * 0.95,
                'acting_price' =>$result['data']['OrigPrice'] * 1.01,
                'market_price' =>$result['data']['Price'],
                'price' =>$result['data']['OrigPrice'],
                'warehouse_id' => 5
            ]);
            $stock =  $this->xilian->query_stock($v,$this->organ);
            $this->model->where(['app_id'=>$this->appID,'earn13'=>$v])->save([
                'stock_num' =>$stock['data'],
                'wholesale_start_num' =>100,
                'lock_start_num' =>1000
            ]);
        }
    }
}